package Bill.app.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.util.Hashtable;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Bill.util.Configuration;

public class Messagerie extends JFrame
    implements ActionListener, WindowListener, ListSelectionListener
{

      Authen authentification;
      Causerie causerie;
	Messagerie(String login,Authen authentification)
        throws IOException
    {
        super((new StringBuilder("BZHMessenger 1.0.0     ")).append(login).append("@BZHMessenger").toString());
        listeAmi = new JList();
      this. authentification=  authentification;
        Discussion = new Hashtable();
        ajouter = new JMenuItem("ajouter un contact");
        chatgroup = new JMenuItem("discussion de group");
        IDChange = new JMenuItem("modifier vos identifiant..");
        Deconnexion = new JMenuItem("Deconnexion");
        this.login = null;
        messagerie = null;
        messagerieLecture = null;
        info = new JLabel();
        logich = new JLabel();
        this.login = login;
        add("South", info);
        info.setVisible(false);
        JMenu fichier = new JMenu("fichier");
        fichier.add(ajouter);
        fichier.addSeparator();
        fichier.add(chatgroup);
        fichier.addSeparator();
        fichier.add(IDChange);
        fichier.addSeparator();
        fichier.add(Deconnexion);
        JMenuBar bar = new JMenuBar();
        bar.add(fichier);
        setJMenuBar(bar);
        listeAmi.setCellRenderer(new LabelListCellRendererM());
        JScrollPane scroller = new JScrollPane(listeAmi);
        scroller.setVerticalScrollBarPolicy(22);
        scroller.setHorizontalScrollBarPolicy(30);
        scroller.setPreferredSize(new Dimension(300, 150));
        scroller.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""), BorderFactory.createEmptyBorder(1, 1, 1, 1)), scroller.getBorder()));
        add("Center", scroller);
        listeAmi.setVisible(true);
        listeAmi.setFont(new Font("Serif", 1, 15));
        ajouter.addActionListener(this);
        Deconnexion.addActionListener(this);
        chatgroup.addActionListener(this);
        listeAmi.addListSelectionListener(this);
        IDChange.addActionListener(this);
        addWindowListener(this);
        setIconImage((new ImageIcon("images.gif")).getImage());
        Configuration.loadConfiguration();
        messagerie = new Socket(Configuration.IPserveur, Configuration.Config_portServeur);
        messagerieLecture = new BufferedReader(new InputStreamReader(messagerie.getInputStream()));
        PrintWriter ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(messagerie.getOutputStream())), true);
        ecriture.println((new StringBuilder("8:")).append(login).toString());
        new MessagerieEcoute(messagerieLecture, login, Etat, Discussion);
        setBackground(new Color(167, 20, 171));
        setSize(500, 600);
        Dimension dimensionecran = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((dimensionecran.width - getWidth()) / 2, (dimensionecran.height - getHeight()) / 2);
        setVisible(true);
        setResizable(false);
        logich.setToolTipText(this.login);
        causerie= new Causerie(listeAmi, logich, Etat);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == ajouter)
            new AJouter(login);
        else
        if(e.getSource() == chatgroup)
            new GroupChat(login, Etat);
        else
        if(e.getSource() == Deconnexion)
            deconnexion();
        else
        if(e.getSource() == IDChange)
            new ChangerIdentifiant(login, logich);
    }

    public void windowActivated(WindowEvent windowevent)
    {
    }

    public void windowClosed(WindowEvent arg0)
    {
        Deconnexion.doClick();
    }

    public void windowClosing(WindowEvent arg0)
    {
        Deconnexion.doClick();
    }

    public void windowDeactivated(WindowEvent windowevent)
    {
    }

    public void windowDeiconified(WindowEvent windowevent)
    {
    }

    public void windowIconified(WindowEvent windowevent)
    {
    }

    public void windowOpened(WindowEvent windowevent)
    {
    }

    public void deconnexion()
    {
        Socket envoi = null;
        Etat = "CLOSED";
        try
        {Configuration.loadConfiguration();
            envoi = new Socket(Configuration.IPserveur, Configuration.Config_portServeur);
        }
        catch(IOException e1)
        {
            e1.printStackTrace();
        }
        PrintWriter ecriture = null;
        try
        {
            ecriture = new PrintWriter(new BufferedWriter(new OutputStreamWriter(envoi.getOutputStream())), true);
        }
        catch(IOException e1)
        {
            e1.printStackTrace();
        }
        String mess = (new StringBuilder("7:")).append(login).toString();
        ecriture.println(mess);
        ecriture.close();
        try
        {
            envoi.close();
        }
        catch(IOException e1)
        {
            
            e1.printStackTrace();
        }
        Etat = "CLOSED";
       if(authentification!=null)
       { authentification.login.setText(login);
       causerie.stop();
    	this.setVisible(false);   
authentification.setVisible(true);
       }else
    	   System.exit(0);
       
    }

    public void valueChanged(ListSelectionEvent a)
    {
        JLabel label = (JLabel)((JList)a.getSource()).getSelectedValue();
        if(label.getToolTipText().compareToIgnoreCase("votre contact est en ligne") == 0)
        {
            String destinataire = label.getText();
            if(!Discussion.containsKey(destinataire))
            {
                info.setVisible(false);
                try
                {
                    Chat ii = new Chat(login, destinataire, " ", Etat, Discussion);
                    Discussion.put(destinataire, ii);
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            } else
            {
                info.setVisible(true);
                info.setText("Discussion dej\340 en cours!");
                ((Chat)Discussion.get(destinataire)).show();
            }
        } else
        {
            info.setVisible(true);
            info.setText("<html><body><h3><code>votre contact est<font color=\"red\" hors ligne </font>impossible de discuter avec lui!<code></h3></body></html>");
        }
    }

    JList listeAmi;
    static String Etat = "OPENED";
    Hashtable Discussion;
    JMenuItem ajouter;
    JMenuItem chatgroup;
    JMenuItem IDChange;
    JMenuItem Deconnexion;
    String login;
    Socket messagerie;
    BufferedReader messagerieLecture;
    JLabel info;
    JLabel logich;

}
